<?php
namespace Apps\BoxEdu\Model;

use Think\Model;

class EduSubjectModel extends \BaseModel {
	public function load($id = "") {
		// dump($id);
		$data = $this->find($id);
		if (!$data) {
			return false;
		}
		$data['end_date'] = date("Y-m-d", strtotime("+3 month"));
		$data['price'] = intval($data['price']);
		/**********课程下的题型***/
		$data['subjectsetting'] = json_decode($data['subjectsetting'], true);
		$data['tikuNavArray'] = json_decode($data['tikuNav'], true);
		$model = BoxModel("edu_questype");
		foreach ($data['subjectsetting'] as $one) {
			$subjectsetting[] = $model->find($one);
		}
		$data['subjectsetting'] = $subjectsetting;
		/*************课程所属科目**********/
		$id = $data['subject_class'];
		$subject_class = BoxModel('edu_subject_class')->where("id='$id'")->find();
		$data['subject_class_name'] = $subject_class['name'];
		$data['picUrl'] = ImgDomain . "/" . $data['picname'];
		/***子课程*/
		//dump($id);
		//视频信息

		$data['childs'] = $this->where(array('parent_id' => $data['subjectid']))->order("order_by")->select();

		/****总课时**/
		$subjectids = array($data['subjectid'] => $data['subjectid']);

		if (is_array($data['childs'])) {
			foreach ($data['childs'] as $one) {
				$subjectids[$one['subjectid']] = $one['subjectid'];
			}

		}
		$subjectids = implode(",", $subjectids);
		//dump($subjectids);exit();
		$data['class_hours'] = BoxModel("edu_knows")->where("subjectid in ($subjectids)")->count();
		return $data;
	}

	/*********课程的章节列表********/
	public function get_section_by_subjectid($subjectid) {
		return $this->get_section("S.sectionsubjectid='$subjectid'");
	}

	/*********章节列表********/
	public function get_section($where = "1", $limit = "", $orderBy = "S.order_by") {
		$model = BoxModel("edu_sections");
		$where .= " and (S.is_show=1)";
		if ($limit) {
			$count = $model->alias("S")->join("edu_subject J on J.subjectid=S.sectionsubjectid")->where($where)->count();
			$page = setPager($count, $limit);
			$limit = $page['firstRow'] . "," . $page['listRows'];
		}
		$re = $model->alias("S")->join("edu_subject J on J.subjectid=S.sectionsubjectid")->where($where)->field("J.*,S.*")->limit($limit)->order($orderBy)->select();
		if ($re && $limit) {
			$data = array(
				'count' => $count,
				'list' => $re,
				'pager' => $page,
			);
		} else {
			$data = $re;
		}
		return $data;
	}

	/*********章节详细********/
	public function get_section_one($id) {
		$data = $this->get_section("S.sectionid='$id'");
		return $data['0'];
	}

	/*********科目列表********/
	public function subject_class() {
		$data = BoxModel("edu_subject_class")->get("is_show=1", "", "type,order_by");
		return $data;
	}

	/*********一个章节的知识点列表********/
	public function get_knows($sectionid) {
		$data = BoxModel("edu_knows")->where("knowssectionid='$sectionid'")->order("order_by")->select();
		foreach ($data as $k => $one) {
			$data[$k]['video_info'] = json_decode($one['video_info'], true);
		}

		return $data;
	}

	/***班级列表*/
	public function class_list() {
		$class_type = dict("subject_class_type");
		$model = BoxModel("edu_subject_class");
		$data = array();
		foreach ($class_type as $k => $one) {
			$row = array();
			$row['name'] = $one;
			$row['list'] = $model->get("is_show=1 and type='$k'", "", "order_by");
			$data[$k] = $row;

		}
		return $data;

	}

	/***班级根*/
	public function rootSubject($subjectid) {
		$data = $this->where(array('subjectid' => $subjectid))->find();
		if ($data['parent_id']) {
			return $this->rootSubject($data['parent_id']);
		} else {
			return $data;
		}
	}

	/***学霸榜 Top 20*/
	public function rank($subjectid, $limit = 10) {
		$data = BoxModel("edu_subject")->where(array('subjectid' => $subjectid))->find();
		$subjectid = $data['parent_id'] ? $data['parent_id'] : $subjectid;
		if ($subjectid) {
			$data = BoxModel("edu_subject_rank")->where(array('subjectid' => $subjectid))->order("num desc")->limit($limit)->select();
		} else {
			$data = BoxModel("system_member")->field("xuebazi as num , user_id")->order("xuebazi desc,user_id desc")->limit($limit)->select();
		}
		$model = BoxModel("system_member");
		foreach ($data as $k => $one) {
			$data[$k]['user'] = $model->load($one['user_id']);
			$data[$k]['user']['nickname'] = $data[$k]['user']['real_name'] ? $data[$k]['user']['real_name'] : $data[$k]['user']['nickname'];
		}
		return $data;
	}

	public function tree() {
		//	header("Content-type: text/html; charset=utf-8");
		$data = $this->get("is_show=1 and status=1", "", "subject_class,order_by");
		foreach ($data as $k => $one) {
			$one['id'] = $one['subjectid'];
			$one['pid'] = $one['parent_id'];
			$one['name'] = $one['subject'];
			$data[$k] = $one;
		}
		$data = getTree($data);
		foreach ($data as $k => $one) {
			$one['subject'] = $one['showname'];
			$data2[$one['subjectid']] = $one;

		}
		return $data2;

	}

	public function ForExamSearch() {
		$array_computer = S('ExamQuery_array_computer');
		$data['3'] = $array_computer['bkjb']; //计算机
		return (array) $data;
	}

}
